package com.apes.fn.crm.customer;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.fn.crm.customerExpand.FnCustomerExpand;
import com.apes.fn.crm.customerExpand.FnCustomerExpandRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.DateUtil;
import com.apes.framework.util.MapUtil;
import com.apes.pi.service.PiManager;
import com.apes.scm.masterdata.car.model.CarConfiguration;
import com.apes.scm.masterdata.car.model.CarModelServiceLevel;
import com.apes.scm.masterdata.car.repository.CarConfigurationRepository;
import com.apes.scm.masterdata.car.repository.CarModelServiceLevelRepository;
import com.apes.scm.masterdata.party.model.AccessProvider;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.AccessProviderRepository;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import com.apes.scm.rbac.repository.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("fnApiService")
public class FnApiService extends DomainService {


    @Autowired
    PartyRoleRepository partyRoleRepository;
    @Autowired
    PiManager piManager;
    @Autowired
    PostRepository postRepository;
    @Autowired
    AccessProviderRepository accessProviderRepository;
    @Autowired
    CarConfigurationRepository carConfigurationRepository;
    @Autowired
    CarModelServiceLevelRepository carModelServiceLevelRepository;

    @Value("${apes.switched:false}")
    private Boolean switched;


    /***
     *  提供展示前端的客户数据接口
     */


    public Map getCustomerDetailInfo(SimpleRequest request) {

        JSONObject tempJson = request.getJO();
        if( tempJson.get("mobile") == null ) throw new RuntimeException("手机号码不能为空！");

        // 允许手动传入接入商
        Map requestMap;
        if( tempJson.get("accessProviderId") != null ||  tempJson.get("vendorCode") != null) {
            requestMap = MapUtil.mapper(
                    "mobile", request.get("mobile"),
                    "vendorCode",  tempJson.get("accessProviderId") != null ? tempJson.get("accessProviderId") : tempJson.get("vendorCode")
            );
        } else {
            requestMap = MapUtil.mapper(
                    "mobile", request.get("mobile")
            );
        }


        JSONObject customer = this.invoke("fn.customer.findOne", requestMap);

        if( customer == null ||  customer.size() == 0) throw new RuntimeException("当前手机号在当前公司下未找到对应的会员信息，手机号码：" + request.get("mobile"));

        PartyRole company = postRepository.findOne(getSession(request).getPostId()).getCompany();
        AccessProvider accessProvider = accessProviderRepository.findByCompany(company.getId());

        return MapUtil.mapper(
                "id", customer.get("id") != null ? customer.get("id") : "",
                "mobile", customer.get("mobile") != null ? customer.get("mobile") : "",
                "realname", customer.get("realname") != null ? customer.get("realname") : "",
                "customerBaseInfo", getCustomerBaseInfo(customer, accessProvider), //基础信息
                "customerBalance", getCustomerBalance(customer, accessProvider) //会员余额
//                "customerConsumeRecord", getCustomerConsumeRecord(customer, accessProvider) //会员消费记录
        );

    }


    public Map getControllerCustomerCarDetailInfo(SimpleRequest request){
        JSONObject tempJson = request.getJO().getJSONObject("node");
        if( tempJson.get("car_license") == null ) throw new RuntimeException("车牌号码不能为空！");
        FnCustomerExpand fnCustomerExpand = SpringManager.getBean(FnCustomerExpandRepository.class).findOne(tempJson.getString("fn_customer_expand_id"));

        // 允许手动传入接入商
        Map requestMap;
        if( fnCustomerExpand.getVendorCode() != null) {
            requestMap = MapUtil.mapper(
                    "carLicense", fnCustomerExpand.getCarLicense(),
                    "vendorCode", fnCustomerExpand.getVendorCode()
            );
        } else {
            requestMap = MapUtil.mapper(
                    "carLicense", fnCustomerExpand.getCarLicense()
            );
        }
        return requestMap;

    }

    /**
     * 后端调用不需要session
     * @param request
     * @return
     */
    public Map getCustomerCarDetailInfoNotSession(SimpleRequest request){
        return getCustomerCarDetailInfo(request);
    }

    /***
     * 提供展示前端车辆数据的接口
     */
    public Map getCustomerCarDetailInfo(SimpleRequest request) {

        JSONObject tempJson = request.getJO();
        if( tempJson.get("carLicense") == null ) throw new RuntimeException("车牌号码不能为空！");

        // 允许手动传入接入商
        Map requestMap;
        if( tempJson.get("accessProviderId") != null ||  tempJson.get("vendorCode") != null) {
            requestMap = MapUtil.mapper(
                    "carLicense", request.get("carLicense"),
                    "vendorCode",  tempJson.get("accessProviderId") != null ? tempJson.get("accessProviderId") : tempJson.get("vendorCode")
            );
        } else {
            requestMap = MapUtil.mapper(
                    "carLicense", request.get("carLicense")
             );
        }


        JSONObject customerCar = this.invoke("fn.customerCar.findOne", requestMap);

        if( customerCar == null || customerCar.size() == 0) throw new RuntimeException("当前车牌号码在当前公司下未找到对应的车辆信息，车牌号码：" + request.get("carLicense"));


        Map returnMap = MapUtil.mapper(
                "id", customerCar.get("id") != null ? customerCar.get("id") : "", // 编码
                "vin", customerCar.get("vin") != null ? customerCar.get("vin") : "", //车架号
                "carModel", customerCar.get("carModel") != null ? customerCar.get("carModel") : "", //车架号
                "carServiceLevel", customerCar.get("carServiceLevel") != null ? customerCar.get("carServiceLevel") : "", //车架号
                // TODO: 车型 厂牌
                "carLicense", customerCar.get("carLicense") != null ? customerCar.get("carLicense") : "", // 车牌号
                "jekunModelCode", customerCar.get("jekunModelCode") != null ? customerCar.get("jekunModelCode") : "", // 配置车型编码
                "carModelLabel", customerCar.get("carModelLabel") != null ? customerCar.get("carModelLabel") : "", // 配置车型名称
                "carInsurance", getCarInsurance(customerCar) // 商务信息
//                "carMaintenanceRecord", getCarMaintenanceRecord(), // 保养记录
//                "carConsumeRecord", getCarConsumeRecord() // 消费记录

        );

        returnMap.putAll(getCarConfigureAndOilInfo(customerCar));

        //车头，车尾，铭牌照片
        request.setData(MapUtil.mapper("carLicense", tempJson.get("carLicense"), "vendorCode", tempJson.get("vendorCode")));
        String pictures = getBean(FnAppCustomerService.class).getPictures(request);
        returnMap.put("pictures", pictures);

        return returnMap;
    }


    /***
     * 1. 基础信息
     * 2. 会员储值
     * 3. 消费记录
     *
     * 会员信息  13113319321/吴先生
     */

    /***
     * 基础信息： 通过接入商和会员编码获取基础信息
     *      * 基础信息
     *      * 注册时间: 2019-12-20
     *        会员级别: VIP会员
     *        注册门店:舆马千里
     *        会员活跃度:活跃会员
     *      * 注册渠道:扫码  平台标签: ********接入商********
     */
    private Map getCustomerBaseInfo(JSONObject param, AccessProvider accessProvider) {


        return MapUtil.mapper(
                "registerDate", param.get("createdAt") != null ?
                        DateUtil.format(new Date(Long.valueOf(param.getString("createdAt") + "000")), DateUtil.SHOW_DATETIME_FORMAT) : "",
//                "registerChannelName", "注册渠道",
//                "registerDeptName", "注册门店",
                "customerLevelName", param.getString("levelLabel"),
                "customerRoleName", param.get("userRole") != null ? param.getJSONObject("userRole").getString("name") : "",
//                "customerActiveName", "会员活跃度",
//                "platformTag", "平台标签",
                "accessProviderName", param.getString("vendorName")
        );

    }


    /***
     * 会员余额： 通过接入商和会员编码获取会员余额
     *
     *      * 会员余额
     *      * 储值余额: 123.00  红包余额: 123.00 积分余额: 123.00
     */
    private Map getCustomerBalance(JSONObject param, AccessProvider accessProvider) {
        return MapUtil.mapper(
                "saveBalance",  param.get("rechargeAmount"), // 储值余额
                "redEnvelopeBalance", param.get("redPacketAmount") // 红包余额
        );
    }


    /***
     * 消费记录： 通过接入商和会员编码获取消费记录
     *      * 消费记录
     *      * 首次消费日期: 2017-07-07  累计消费金额: 7888.00
     *      * 最近消费日期: 2017-07-07  平均消费金额: 1500.00消费次数: 23
     */
//    private Map getCustomerConsumeRecord(JSONObject param, AccessProvider accessProvider) {
//        return MapUtil.mapper(
//                "firstConsumeDate", DateUtil.format(DateUtil.SHOW_DATE_FORMAT), // 首次消费日期
//                "recentConsumeDate", DateUtil.format(DateUtil.SHOW_DATE_FORMAT), // 最近消费日期
//                "accumulateConsumeAmount", 0, // 累计消费金额
//                "averageConsumeAmount", 0, // 平均消费金额
//                "consumeCount", 0 // 消费次数
//        );
//    }


    /***
     * 1. 基础信息
     * 2. 商务信息
     * 3. 保养信息
     * 4. 保养记录
     * 5. 消费记录
     * 基础信息
     * 车型配件代码:CGTOE15JC 发动机型号: 274 920 发动机号码; 10244355 驱动方式:全时四驱
     * 前轮胎规格: 225/50 R17 后轮胎规格: 225/50 R17 排量:2.0T 服务等级: D级
     * 变速器:手自一体变速器(AMT) 变速箱型号; BUA;BLKA 变速箱类型:自动发动机启停技术:有
     * 生产年份: 2015 年款: 2015 核载人数:5
     *
     * 商务信息
     * 商业险到期: 2019-12-05 交强险到期: 2019-12-05
     * 年审到期: 2019-12-05
     * 保养信息
     * 基础油:半合成/0W-20/6.3L
     * 适用类型:车况良好-代用/半台成/0W-20
     * 适用类型:车况不好/半合成/0W-30
     * 变速箱: KT017/PDK-BG/12L
     *
     * 保养记录
     * 上次保养里程; 9000公里 上次保养日期: 2019-06-05
     * 建议保养里程: 15000
     * 建议保养日期: 2019-12-05
     *
     * 消费记录
     * 上次到店日期: 2019-12-05 累计消费金额: 1600元
     * 行驶里程; 92685.43 到店次数:4
     */

    /***
     * 基础信息
     *      * 车型配件代码:CGTOE15JC 发动机型号: 274 920 发动机号码; 10244355 驱动方式:全时四驱
     *      * 前轮胎规格: 225/50 R17 后轮胎规格: 225/50 R17 排量:2.0T 服务等级: D级
     *      * 变速器:手自一体变速器(AMT) 变速箱型号; BUA;BLKA 变速箱类型:自动 发动机启停技术:有
     *      * 生产年份: 2015 年款: 2015 核载人数:5
     */


    /***
     * 获取额外数据(基础信息)
     */
    private Map getCarConfigureAndOilInfo(Map param) {
        if (param.get("jekunModelCode") != null) {
            if (!switched){
                try {
                    JSONObject baseInfo = piManager.synErp(
                            "ipmServicePlanService.getCarDetailInfoForNew", MapUtil.mapper("PZCXID", param.get("jekunModelCode")));

                    if (baseInfo != null && baseInfo.get("obj") != null) {
                        return MapUtil.mapper(
                                "carBaseInfo", getCarBaseInfo(JSONObject.parseObject(baseInfo.getString("obj"))),
                                "carMaintenanceInfo", getCarMaintenanceInfo(JSONObject.parseObject(baseInfo.getString("obj")))
                        );
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else {
                CarConfiguration carConfiguration = carConfigurationRepository.findOne((String) param.get("jekunModelCode"));

                if (carConfiguration != null ) {
                    return MapUtil.mapper(
                            "carBaseInfo", getCarBaseInfo(carConfiguration),
                            "carMaintenanceInfo", getCarMaintenanceInfo(carConfiguration)
                    );
                }
            }

        }

        return MapUtil.mapper(
                "carBaseInfo", new HashMap(),
                "carMaintenanceInfo", new HashMap()
        );
    }

    /**
     * 获取车型服务等级
     * @param
     * @return
     */
    public JSONObject getCarServiceLevel(String jekunModelCode){
        CarConfiguration carConfiguration = carConfigurationRepository.findOne(jekunModelCode);
        CarModelServiceLevel carModelServiceLevel = carConfiguration!=null?carModelServiceLevelRepository.findByCarModel(carConfiguration.getCarModel()):null;
        JSONObject result = new JSONObject();
        if (carModelServiceLevel!=null)result.put("CarServiceLevelName",carModelServiceLevel.getCarServiceLevel().getName());
        if (carModelServiceLevel!=null)result.put("CarServiceLevelId",carModelServiceLevel.getCarServiceLevel().getId());
        result.put("validFlag","valid");
        if (carConfiguration!=null&&carConfiguration.getCarModel()!=null)result.put("carModel",carConfiguration.getCarModel().getId());
        if (carConfiguration!=null)result.put("validFlag",carConfiguration.getValidFlag());
        return result;
    }


    /***
     * 基础信息
     * 车型配件代码:CGTOE15JC 发动机型号: 274 920 发动机号码; 10244355 驱动方式:全时四驱
     * 前轮胎规格: 225/50 R17 后轮胎规格: 225/50 R17 排量:2.0T 服务等级: D级
     * 变速器:手自一体变速器(AMT) 变速箱型号; BUA;BLKA 变速箱类型:自动发动机启停技术:有
     * 生产年份: 2015 年款: 2015 核载人数:5

     * 保养信息
     * 基础油:半合成/0W-20/6.3L
     * 适用类型:车况良好-代用/半台成/0W-20
     * 适用类型:车况不好/半合成/0W-30
     * 变速箱: KT017/PDK-BG/12L
     */

//-- FDJHM
//    驱动方式(0:前轮驱动;1:后轮驱动;2:四轮驱动;3:适时四驱;4:全时四驱;5:分时四驱;6:电动四驱)
//
//    SELECT T.PZCXID, T.PZCXMC, T.CXPJID, T.FDJXH,
//    DECODE( T.QDFS, '0', '前轮驱动', '1', '后轮驱动','2','四轮驱动','3','适时四驱','4','全时四驱','5','分时四驱','6','电动四驱','无'  ) QDFS，
//    T.QLTGG, T.HLTGG, T.FDJPL,
//            ( SELECT A.CLFWDJID FROM MAS_CXFWDJ A WHERE A.CHXID = T.CHXID ) CLFWDJID,
//            ( SELECT A.CLFWDJMC FROM MAS_CXFWDJ A WHERE A.CHXID = T.CHXID ) CLFWDJMC,
//    T.BSQMS, T.BSXXH, T.BSXLB, t.FDJQTJS, t.SCNF, t.NK, t.ZWSL
//    FROM MAS_QCCXPZ T
//    WHERE T.PZCXID = 'CPD0218M00020';
    private Map getCarBaseInfo(JSONObject param) {

        return MapUtil.mapper(
                "carConfiguration",  param.get("PZCXID") != null ? (param.getString("PZCXID") + " / " + param.getString("PZCXMC") ) : "", //配置车型
                "carModel",  param.get("CHXID") != null ? (param.getString("CHXID") + " / " + param.getString("CHXMC") ) : "", //车型
                "carLabel",  param.get("QCCPID") != null ? (param.getString("QCCPID") + " / " + param.getString("QCCPMC") ) : "", //厂牌
                "partsCode", param.get("CXPJID") != null ? param.get("CXPJID") : "", //车型配件代码
                "engineModel", param.get("FDJXH") != null ? param.get("FDJXH") : "", // 发动机型号
                "engineCode", "", // 发动机号码
                "drive", param.get("QDFS") != null ? param.get("QDFS") : "", // 驱动方式
                "frontWheelStandard", param.get("QLTGG") != null ? param.get("QLTGG") : "", // 前轮胎规格
                "rearWheelStandard", param.get("HLTGG") != null ? param.get("HLTGG") : "", // 后轮胎规格
                "sweptVolume", param.get("FDJPL") != null ? param.get("FDJPL") : "", // 排量
                "carServiceLevelName", param.get("CLFWDJMC") != null ? param.get("CLFWDJMC") : "", //车辆服务等级
                "transmissionModel", param.get("BSXXH") != null ? param.get("BSXXH") : "", //变速箱型号
                "transmissionDescription", param.get("BSQMS") != null ? param.get("BSQMS") : "", //变速器
                "transmissionType", param.get("BSXLB") != null ? param.get("BSXLB") : "", //变速箱类型
                "STT", param.get("FDJQTJS") != null ? param.get("FDJQTJS") : "", // 发动机启停技术
                "produceYear", param.get("SCNF") != null ? param.get("SCNF") : "", // 生产年份
                "yearType", param.get("NK") != null ? param.get("NK") : "", // 年款
                "passengerQty", param.get("ZWSL") != null ? param.get("ZWSL") : "" // 核载人数
        );
    }
    private Map getCarBaseInfo(CarConfiguration carConfiguration) {
        CarModelServiceLevel carModelServiceLevel = carModelServiceLevelRepository.findByCarModel(carConfiguration.getCarModel());

        return MapUtil.mapper(
                "carConfiguration",  carConfiguration.getId()+ " / " + carConfiguration.getName() , //配置车型
                "carModel",  carConfiguration.getCarModel().getId() + " / " + carConfiguration.getCarModel().getName() , //车型
                "carLabel", carConfiguration.getCarLabel().getId() + " / " + carConfiguration.getCarLabel().getName(), //厂牌
                "carLabelOrModel", carConfiguration.getCarLabel().getName() + " / " + carConfiguration.getCarModel().getName(), //厂牌
                "partsCode", carConfiguration.getPartsCode(), //车型配件代码
                "engineModel", carConfiguration.getEngineModel(), // 发动机型号
                "engineCode", "", // 发动机号码
                "drive", carConfiguration.getDrive()!=null?carConfiguration.getDrive().getDescription():"", // 驱动方式
                "frontWheelStandard", carConfiguration.getFrontWheelStandard(), // 前轮胎规格
                "rearWheelStandard", carConfiguration.getRearWheelStandard(), // 后轮胎规格
                "sweptVolume", carConfiguration.getSweptVolume(), // 排量
                "carServiceLevelName",carModelServiceLevel==null?null: carModelServiceLevel.getCarServiceLevel().getName(), //车辆服务等级
                "transmissionModel",carConfiguration.getTransmissionModel(), //变速箱型号
                "transmissionDescription", carConfiguration.getTransmissionDescription(), //变速器
                "transmissionType", "", //变速箱类型
                "STT", carConfiguration.isSTT(), // 发动机启停技术
                "produceYear", carConfiguration.getProduceYear(), // 生产年份
                "yearType", carConfiguration.getNK(), // 年款
                "passengerQty",carConfiguration.getPassengerQty() // 核载人数
        );
    }


    /***
     * 保养信息
     *      * 基础油:半合成/0W-20/6.3L
     *      * 适用类型:车况良好-代用/半台成/0W-20
     *      * 适用类型:车况不好/半合成/0W-30
     *      * 变速箱: KT017/PDK-BG/12
     */
    private Map getCarMaintenanceInfo(JSONObject param) {
        JSONArray items = param.getJSONArray("JCYLXArray");
        if (items != null && items.size() > 0) {
            Map returnMap = new HashMap();
            List<String> suitType = new ArrayList();
            returnMap.put("suitType", suitType);
            for (int i = 0, len = items.size(); i < len; i++) {
                JSONObject tempJson = items.getJSONObject(i);
                if (i == 0) {
                    returnMap.put("baseOil",(tempJson.get("JCYLX") != null ? tempJson.getString("JCYLX") : "无") + "/" +
                            (tempJson.get("GG") != null ? tempJson.getString("GG") : "无") + "/" +
                            (tempJson.get("PJYL") != null ? tempJson.getString("PJYL") + "L" : "无"));
                    returnMap.put("gearbox", (tempJson.get("JTXH") != null ? tempJson.getString("JTXH") : "无") + "/" +
                            (tempJson.get("YPXH") != null ? tempJson.getString("YPXH") : "无") + "/" +
                            (tempJson.get("YPYL") != null ? tempJson.getString("YPYL") + "L" : "无"));
                }
                suitType.add((tempJson.get("SYLX") != null ? tempJson.getString("SYLX") : "无") + "/" +
                        (tempJson.get("SYJCYLX") != null ? tempJson.getString("SYJCYLX") : "无") + "/" +
                        (tempJson.get("SYGG") != null ? tempJson.getString("SYGG") : "无"));
            }
            return returnMap;
        } else {
            List list = new ArrayList();
            return MapUtil.mapper(
                    "baseOil", "", // 基础油
                    "suitType", list, // 适用类型
                    "gearbox", "" // 变速箱
            );
        }
    }

    private Map getCarMaintenanceInfo(CarConfiguration carConfiguration) {
        JSONObject maintainPeriod = this.invoke("dc.maintainPeriod.findByCXID", MapUtil.mapper("PZCXID", carConfiguration.getId()));
        JSONArray items = maintainPeriod.getJSONArray("JCYLXArray");
        if (items != null && items.size() > 0) {
            Map returnMap = new HashMap();
            List<String> suitType = new ArrayList();
            returnMap.put("suitType", suitType);
            for (int i = 0, len = items.size(); i < len; i++) {
                JSONObject tempJson = items.getJSONObject(i);
                if (i == 0) {
                    returnMap.put("baseOil",(tempJson.get("JCYLX") != null ? tempJson.getString("JCYLX") : "无") + "/" +
                            (tempJson.get("GG") != null ? tempJson.getString("GG") : "无") + "/" +
                            (tempJson.get("PJYL") != null ? tempJson.getString("PJYL") + "L" : "无"));
                    returnMap.put("gearbox", (tempJson.get("JTXH") != null ? tempJson.getString("JTXH") : "无") + "/" +
                            (tempJson.get("YPXH") != null ? tempJson.getString("YPXH") : "无") + "/" +
                            (tempJson.get("YPYL") != null ? tempJson.getString("YPYL") + "L" : "无"));
                }
                suitType.add((tempJson.get("SYLX") != null ? tempJson.getString("SYLX") : "无") + "/" +
                        (tempJson.get("SYJCYLX") != null ? tempJson.getString("SYJCYLX") : "无") + "/" +
                        (tempJson.get("SYGG") != null ? tempJson.getString("SYGG") : "无"));
            }
            return returnMap;
        } else {
            List list = new ArrayList();
            return MapUtil.mapper(
                    "baseOil", "", // 基础油
                    "suitType", list, // 适用类型
                    "gearbox", "" // 变速箱
            );
        }    }

    /***
     * 商务信息
     *
     *      * 商业险到期: 2019-12-05 交强险到期: 2019-12-05
     *      * 年审到期: 2019-12-05
     */
    private Map getCarInsurance(JSONObject param) {
        if( param.get("insuranceTrack") != null) {
            return MapUtil.mapper(
                    "commercialDateEnd", param.getJSONObject("insuranceTrack").get("lastCommerceEndDate"), // 商业险到期
                    "strongDateEnd", param.getJSONObject("insuranceTrack").get("lastForceEndDate") // 交强险到期
            );
        } else {
            return MapUtil.mapper(
                    "commercialDateEnd", "", // 商业险到期
                    "strongDateEnd", "" // 交强险到期
            );
        }
    }

    /***
     * 保养记录
     *      * 上次保养里程; 9000公里 上次保养日期: 2019-06-05
     *      * 建议保养里程: 15000
     *      * 建议保养日期: 2019-12-05
     */
    private Map getCarMaintenanceRecord() {
//        CarMaintenance carMaintenance = new CarMaintenance();
        return MapUtil.mapper(
                "lastMaintenanceDate", 0, // 上次保养里程
                "lastMaintenanceMileage", DateUtil.format(DateUtil.SHOW_DATE_FORMAT), // 上次保养日期
                "nextMaintenanceMileage", 0, // 建议保养里程
                "nextMaintenanceDate", DateUtil.format(DateUtil.SHOW_DATE_FORMAT) // 建议保养日期

        );
    }

    /***
     * 消费记录
     *      * 上次到店日期: 2019-12-05 累计消费金额: 1600元
     *      * 行驶里程; 92685.43 到店次数:4
     */
    private Map getCarConsumeRecord() {
//        CustomerCar customerCar = new CustomerCar();
//        CarMaintenance carMaintenance = new CarMaintenance();

        return MapUtil.mapper(
                "lastVisitsDate", DateUtil.format(DateUtil.SHOW_DATE_FORMAT),
                "totalSpendAmt", 0,
                "drivenedMileage", 0,
                "visitsCount", 0
        );

    }


    /***
     * 获取车辆基础信息
     */
    public JSONObject getCarDetailInfoForNew(SimpleRequest request) {
        JSONObject param = request.getJO();
        if (param.get("PZCXID") == null) throw new RuntimeException("配置车型编码不能为空");
        try{
            if (!switched){
                JSONObject baseInfo = piManager.synErp(
                        "ipmServicePlanService.getCarDetailInfoForNew", MapUtil.mapper("PZCXID", param.getString("PZCXID")));

                if (!baseInfo.getBoolean("success")) {
                    throw new RuntimeException(baseInfo.getString("msg"));
                } else {
                    JSONObject result = JSONObject.parseObject(baseInfo.get("obj").toString());
                    return result;
                }

            }else {
                return getCarDetailInfo(request);
            }
        } catch (Exception e) {
            throw new RuntimeException("未查询到对应的配置车型信息，配置车型编码：" + param.getString("PZCXID"));
        }
    }

    /**
     * 获取车辆基础信息(老切新数据)
     * @param request
     * @return
     */
    public JSONObject getCarDetailInfo(SimpleRequest request){
        JSONObject jo = request.getJO();
        String pzcxid = jo.getString("PZCXID");
        CarConfiguration carConfiguration = carConfigurationRepository.findOne(pzcxid);
        return newFieldToOld(carConfiguration);
    }

    public JSONObject newFieldToOld(CarConfiguration carConfiguration){
        CarModelServiceLevel carModelServiceLevel = carModelServiceLevelRepository.findByCarModel(carConfiguration.getCarModel());
        if (carModelServiceLevel==null) carModelServiceLevel = SpringManager.getBean(CarModelServiceLevelRepository.class).findOne("20");
        JSONObject maintainPeriod = this.invoke("dc.maintainPeriod.findByCXID", MapUtil.mapper("PZCXID", carConfiguration.getId()));
        JSONObject result = new JSONObject();
        result.put("RLLX",carConfiguration.getFuelType());
        result.put("QLTGG",carConfiguration.getFrontWheelStandard());
        result.put("PZCXMC",carConfiguration.getName());
        result.put("QDFS",carConfiguration.getDrive()!=null?carConfiguration.getDrive().getDescription():"");
        result.put("FDJXH",carConfiguration.getEngineModel());
        result.put("HLTGG",carConfiguration.getRearWheelStandard());
        result.put("PZCXID",carConfiguration.getId());
        result.put("SCNF",carConfiguration.getProduceYear());
        result.put("BSQMS",carConfiguration.getTransmissionDescription());
        result.put("QCCPID",carConfiguration.getCarLabel().getId());
        result.put("FDJPL",carConfiguration.getSweptVolume());
        result.put("JCYLXArray",maintainPeriod.get("JCYLXArray"));//基础油类型
//        result.put("BSXLB",);
        result.put("CXPJID",carConfiguration.getPartsCode());
        result.put("CHXMC",carConfiguration.getCarModel().getName());
        result.put("CHXID",carConfiguration.getCarModel().getId());
        result.put("ZWSL",carConfiguration.getPassengerQty());
        result.put("CLFWDJMC",carModelServiceLevel.getCarServiceLevel().getName());
        result.put("QCCPMC",carConfiguration.getCarLabel().getName());
        result.put("NK",carConfiguration.getNK());
        result.put("BSXXH",carConfiguration.getTransmissionModel());
        result.put("CLFWDJID",carModelServiceLevel.getCarServiceLevel().getId());
        return result;
    }
}

